# -*- coding:utf-8 -*-
# @FileName  :dhcc_per_1_report.py
# @Time      :2020/11/17 14:52
# @Author    :Zhoujirong
# @Desc      :个人页面查询报告主流程
import parameterized
import datetime,time
from common.log import Log
from biz.flow.per_report import PerReportRequest
from biz.cases.base_case import BaseCase
from common.params_operator import Param
from common.utils import get_redis
from common.utils import download


class TestPerReport(BaseCase):
    '''个人查询报告主流程测试用例'''

    data_file_name = 'per_report.yaml'

    @classmethod
    def setUpClass(cls):
        cls.redis = get_redis()
        # cls.mysql = get_mysql()
        cls.cookie = BaseCase().get_token()

    def test_a_info_create(self):
        '''添加客户信息'''
        clientName = self.file_data.get('clientName')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        info_check = self.file_data.get('info_check')
        ret = PerReportRequest.per_info(clientName,certType,certNo,assocbsnssData,note,queryFormat,resultType,qryReason,queryControlWarningType,serialNumber,self.cookie)
        Log.info(ret)
        self.assertEqual(info_check, ret.resultMsg, '接口调用失败')

    def per_archive_create(self):
        '''添加授权档案'''
        archiveType = self.file_data.get('archiveType')
        Num = self.file_data.get('Num')
        clientName = self.file_data.get('clientName')
        cretType = self.file_data.get('cretType')
        cretNo = self.file_data.get('cretNo')
        startDate = datetime.date.today()
        expireDate = datetime.date.today()
        note2 = self.file_data.get('note')
        paperfileid = self.file_data.get('paperfileid')
        filedescribe = self.file_data.get('filedescribe')
        ret = PerReportRequest.archive_create(archiveType, Num, clientName, cretType, cretNo, startDate, expireDate,
                                              note2, paperfileid, filedescribe, self.cookie)
        Log.info(ret)
        self.assertEqual('00000000', ret.code)
        return ret.msg

    def save_paper(self):
        '''保存授权文档'''
        paperfileid = self.file_data.get('paperfileid')
        filedescribe = self.file_data.get('filedescribe')
        archiveid = self.per_archive_create()
        ret = PerReportRequest.save_paper(paperfileid, filedescribe, archiveid, self.cookie)
        Log.info(ret)
        self.assertEqual('00000000', ret.code, '接口调用失败')
        return archiveid


    def per_checkinfo(self):
        '''查询是够需要授权'''
        clientName = self.file_data.get('clientName')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        archiveId = self.save_paper()
        ret = PerReportRequest.per_checkinfo(clientName, certType, certNo, assocbsnssData, note, queryFormat, resultType,
                                           qryReason,
                                           queryControlWarningType, serialNumber, archiveId,self.cookie)
        Log.info(ret)
        self.assertEqual('10200',ret.resCode)
        return archiveId


    def per_inquire(self):
        '''查询本地是否有报告、同步授权'''
        clientName = self.file_data.get('clientName')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        archiveId = self.per_checkinfo()
        rekUser = self.file_data.get('rekUser')
        rekPasword = self.file_data.get('rekPasword')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = PerReportRequest.per_inquire(clientName, certType, certNo, assocbsnssData, note, queryFormat, resultType,
                                           qryReason,
                                           queryControlWarningType, serialNumber, archiveId, rekUser, rekPasword,
                                           checkWay, autharchiveId, self.cookie)
        Log.info(ret)
        self.assertIsNotNone(ret.checkId)
        return ret.checkId,archiveId

    @parameterized.parameterized.expand(Param.from_excel('per_report_asyn_data.xls'))
    def test_per_inquire_asyn_params(self,clientName,
                                    certType,
                                    certNo,
                                    assocbsnssData,
                                    note,
                                    queryFormat,
                                    resultType,
                                    qryReason,
                                    queryControlWarningType,
                                    serialNumber,
                                    rekUser,
                                    rekPasword,
                                    checkWay,check,desc):
        self._testMethodDoc = desc #用例描述
        archiveId = self.per_checkinfo()
        autharchiveId = archiveId
        ret = PerReportRequest.per_inquire(clientName, certType, certNo, assocbsnssData, note, queryFormat, resultType,
                                           qryReason,
                                           queryControlWarningType, serialNumber, archiveId, rekUser, rekPasword,
                                           checkWay, autharchiveId, self.cookie)
        Log.info(ret)
        self.assertIn(check,ret.resMsg)


    def per_show_report(self):
        '''查询报告'''
        clientName = self.file_data.get('clientName')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = str.zfill('0',2)
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        checkId,archiveId = self.per_inquire()
        rekUser = self.file_data.get('rekUser')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = PerReportRequest.per_show_report(clientName, certType, certNo, assocbsnssData, note, queryFormat,
                                               resultType,
                                               qryReason, queryControlWarningType, serialNumber, archiveId,
                                               autharchiveId, checkId,
                                               rekUser, checkWay, self.cookie)
        Log.info(ret)
        self.assertIsNotNone(ret.url)
        return ret.url

    def test_b_reveal_report(self):
        '''展示报告'''
        recordId = self.per_show_report()[30:]
        creditId = None
        ret = PerReportRequest.per_show_credit(recordId, creditId, self.cookie)
        Log.info(ret)
        name = '个人信用报告dhcc_per_1_reportDtest_b_reveal_report%s.html' % time.strftime('%Y%m%d%H%M%S', time.localtime())
        size = download(name, ret)
        Log.info('文件大小为:%s' % size)
        self.assertNotEqual(0, size)

    def per_inquire_local(self):
        '''本地有报告'''
        clientName = self.file_data.get('clientName')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        archiveId = self.per_checkinfo()
        rekUser = self.file_data.get('rekUser')
        rekPasword = self.file_data.get('rekPasword')
        checkWay = self.file_data.get('checkWay')
        autharchiveId = archiveId
        ret = PerReportRequest.per_inquire(clientName, certType, certNo, assocbsnssData, note, queryFormat, resultType,
                                           qryReason,
                                           queryControlWarningType, serialNumber, archiveId, rekUser, rekPasword,
                                           checkWay, autharchiveId, self.cookie)
        Log.info(ret)
        self.assertEqual('10401',ret.resCode)
        self.assertEqual('本地存在信用报告，是否进行展示?',ret.resMsg)
        return ret.checkId, archiveId

    def test_c_local_report(self):
        '''查询本地报告'''
        clientName = self.file_data.get('clientName')
        certType = self.file_data.get('certType')
        certNo = self.file_data.get('certNo')
        assocbsnssData = self.file_data.get('assocbsnssData')
        note = self.file_data.get('note')
        queryFormat = '00'
        resultType = self.file_data.get('resultType')
        qryReason = self.file_data.get('qryReason')
        queryControlWarningType = self.file_data.get('queryControlWarningType')
        serialNumber = self.file_data.get('serialNumber')
        checkId,archiveId = self.per_inquire_local()
        autharchiveId = archiveId
        rekUser = self.file_data.get('rekUser')
        checkWay = self.file_data.get('checkWay')
        ret = PerReportRequest.local_report(clientName,
                        certType,
                        certNo,
                        assocbsnssData,
                        note,
                        queryFormat,
                        resultType,
                        qryReason,
                        queryControlWarningType,
                        serialNumber,
                        archiveId,
                        autharchiveId,
                        checkId,
                        rekUser,
                        checkWay,self.cookie)
        Log.info(ret)
        self.assertEqual('00',ret.resultCode)
        self.assertEqual('OK',ret.resultMsg)
        self.assertIn('inquire/revealReport?recordId=',ret.url)

    def test_d_prevalidate(self):
        '''查询预校验'''
        pre = self.file_data.get('pre')
        ret = PerReportRequest.prevalidate(pre,self.cookie)
        Log.info(ret)
        self.assertEqual('C0000',ret.queryControlWarningType)
        self.assertEqual('10000',ret.resCode)
        self.assertEqual('查询控制验证通过',ret.resMsg)











if __name__ == "__main__":
    pass